<?php


namespace app\common\util;

class HttpUtil
{
    /**
     * @param $url
     * @return mixed|string
     */
    public static function httpGet($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);//设置需要抓取的url
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//设置curl参数，1表示true，让curl_exec执行结果转为字符串，而不是直接输出
        $headerArray = array("Connection: Keep-Alive", "charset=utf-8", "Accept-Language:zh-CN", "Cache-Control: no-cache");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);//设置http头字段数组属性，
        //如果是https协议请求，需设置ssl验证为false，不验证证书和hosts
        if (substr($url, 0, 5) == "https") {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        $timeout = 30;
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//设置连接等待时间,单位秒
        curl_setopt($ch, CURLOPT_HTTPGET, true);//默认是 GET。
        //执行获取结果
        try {
            $output = curl_exec($ch);//运行curl，请求地址
            //释放curl
            curl_close($ch);
            //解析数据
            return json_decode($output, true);
        } catch (\Exception $ex) {
            return $ex->getMessage();
        }
    }

    /**
     * @param string $url 请求路径
     * @param string $parameters 请求参数
     * @param array $extra 支付证书
     * @return array|mixed|string
     */
    public static function httpPost(string $url, string $parameters, array $extra = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);//设置需要抓取的url
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//设置curl参数，1表示true，让curl_exec执行结果转为字符串，而不是直接输出
        $headerArray = array("Connection: Keep-Alive", "charset=utf-8", "Accept-Language:zh-CN", "Cache-Control: no-cache");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);//设置http头字段数组属性，
        //如果是https协议请求，需设置ssl验证为false，不验证证书和hosts
        if (substr($url, 0, 5) == "https") {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        if (is_array($extra) && count($extra) > 0) {
            curl_setopt($ch, CURLOPT_SSLVERSION, 1);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');
            foreach ($extra as $opt => $value) {
                if (strpos($opt, 'CURLOPT_') !== false) {
                    curl_setopt($ch, constant($opt), $value);
                } elseif (is_numeric($opt)) {
                    curl_setopt($ch, $opt, $value);
                }
            }
        }
        $timeout = 30;
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//设置连接等待时间,单位秒
        curl_setopt($ch, CURLOPT_POST, true);//设置为post，
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);//全部数据使用 "POST" 操作来发送。
        //执行获取结果
        try {
            $output = curl_exec($ch);//运行curl，请求地址
            //释放curl
            curl_close($ch);
            //解析数据
            if (isXml($output)) {
                $result = xmlToArray($output);
            } else
                $result = json_decode($output, true);
            return $result;
        } catch (\Exception $ex) {
            return $ex->getMessage();
        }
    }
}